<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<meta http-equiv="pragma" content="no-cach" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />

<title>无标题文档</title>
<style>
body{margin:0px;  }
li{list-style-type:hiragana;}
@font-face {
				font-family: 'inconsolata';
				src: url('../inc/inconsolata.woff') format('woff');
				font-weight: normal;
				font-style: normal;
			}

</style>
<script src="../inc/tools.build.js" type="text/javascript"></script>
<script src="../o/jquery-2.1.1.js"></script>
<script src="../AppVideo/TOOLS.Status.js"></script>
<script src="../AppVideo/mouse.js"></script>
<script src="../AppVideo/Video.lib.js"></script>
<script src="../AppVideo/initPage.js"></script>
<script src="../AppVideo/plug_volume.js"></script>
<script src="../AppVideo/_subtitle_H_.js"></script>
<script src="../AppVideo/plug_subtitle3.js"></script>
 

<?php
/*fileName: 
 *internal version: 0001
 *version : 1.0.0 [programming] [official version]
 *author : HeShengyu
 *Date : 2014.07.21 
 *email: koolfer@163.com
 *end date:2014.08.23
 *http://www.cnblogs.com/ssnowy/
*/
?>

</head>
<body>
<?php
  //php代码放到script标签就不会不空白现象出现
require_once 'appInc.php';
$filename =preg_replace(  array("/\\\\+/","/\/+/"),array("/","/") ,$_REQUEST['filename']);
$filename = preg_replace('/\.\.\//','',$filename);
if($filename==''){
$filename ='qjwq.txt' ;	
}

$qjwq=_frdf( $filename );
//matchs[$i][0]匹配整个
//  matchs[$i][1] === $1 matchs[$i][2] === $2
//$i为找到的匹配个数
$subtitle = _time2sec($qjwq);
//echo pre_print_r($subtitle);
//echo pre_print_r($qjwq);

function _pathtosimpleName($s){
    return preg_replace('/(.{0,}?)([0-9a-zA-Z\-_\.\$]{0,})(html|php|htm|txt)$/','\\2\\3',$s);	
	
}
$ms = array();
preg_match_all(
'/\[([0-9]{1,})?(?:[:]?)([0-9]{2,})(?:[:]?)([0-9]{2,})(?:\.)([0-9]{0,})\](.*)$/m' ,  $qjwq ,
$ms);
$items=mapms($ms);
$exportSubTitle =  items2sec($items)  ;
$exportSubTitle= implode("\r\n",$exportSubTitle);
echo $exportSubTitle;
if($exportSubTitle<>''){
echo _fwf('./____'._pathtosimpleName($filename).'.lrc.txt',$exportSubTitle)?'ok':'failed';
}else{
    echo '歌词格式不对';	
}
function items2sec($arr){
    if(!is_array($arr)){return false;}
	$retarr=array();
	
	
	$i =0 ;
	$_temparr=array();
	foreach($arr as $k=>$v){
    $h = $v[1];
	$m = $v[2];
	$s = $v[3];
	$ms= $v[4];
	if(  $v[1]==''){
	     $h = 0;	
	}
	if(  $v[2]==''){
	     $m = 0;	
	}
	if(  $v[3]==''){
	     $s = 0;	
	}
	
	$_temparr[]=(($h*60*60+$m*60 +$s)*1000 +$ms*10);
	 
	$i++;
	}
	
	$j=0;
    foreach($arr as $k=>$v){
    $h = $v[1];
	$m = $v[2];
	$s = $v[3];
	$ms= $v[4];
	if(  $v[1]==''){
	     $h = 0;	
	}
	if(  $v[2]==''){
	     $m = 0;	
	}
	if(  $v[3]==''){
	     $s = 0;	
	}
	$stime =($h*60*60+$m*60 +$s)*1000 +$ms*10 ;
	$endtime = $j+1<$i ? $_temparr[$j+1] : $_temparr[$j]+8000;
	
	if($j<$i-1 and $j>0){
	$retarr[]=formatJSON($stime,$endtime,$v[5]).',';
	  
	}elseif($j==0){
	$retarr[]='[ '.formatJSON($stime,$endtime,$v[5]).',';	
	}elseif($j>=$i-1){
	$retarr[]=formatJSON($stime,$endtime,$v[5])." ]";		
	}
	 
	$j++;
	}
 	return $retarr;
}
function formatJSON($s,$e,$str){
	return '{"s":'.$s.',"e":'.$e.',"lyric":"'.preg_replace( '/\r*\n*/','',$str).'"}';	
}

//二维 数组变换  $arr[$i][$j] --> $arr[$j][$i]
function mapms($ms){
    if(!is_array($ms)){return false;}
	$prev = 0;
	$i = 0;
	$retarr = array();
    foreach($ms as $k => $v ){
		//$step = abs($v -$prev);
		//$prev = $v;
		//secho($v);
		$j = 0;
		foreach($v as $kj => $vj){
		$j++;
		$retarr[$kj][$i]=$vj;
		//echo $j.'****<br>';	
		}
		
		//echo $i.'__________<br>';
		$i++;
    }
	return $retarr;	
}



function _time2sec($s){

$s = preg_replace_callback(
'/\[([0-9]{1,})?(?:[:]?)([0-9]{2,})(?:[:]?)([0-9]{2,})(?:\.)([0-9]{0,})\]/','_time2secCB',$s )	;
return $s;	
}
 
function _time2secCB($macths){
	//secho($macths);
	$h = $macths[1];
	$m = $macths[2];
	$s = $macths[3];
	$ms= $macths[4];
	if($macths[1]===''){
	    $h = 0;	
	}
	if($macths[2]===''){
	    $m = 0;	
	}
	if($macths[3]===''){
	    $s = 0;	
	}
	return ($h*60*60+$m*60 +$s)*1000 +$ms*10;

}
?> 
</body>
</html>